package com.xitang.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xitang.common.bean.BizUser;
import com.xitang.common.bean.SysUser;
import com.xitang.common.mapper.BizUserMapper;
import com.xitang.common.mapper.SysUserMapper;
import com.xitang.common.service.IUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private BizUserMapper bizUserMapper;

    @Override
    public List<SysUser> listSysUsers() {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        return sysUserMapper.selectList(wrapper);
    }

    @Override
    public List<BizUser> listBizUsers(String username, String vxUsername) {
        QueryWrapper<BizUser> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(username)) {
            wrapper.eq("username", username);
        }
        if (StringUtils.isNotEmpty(vxUsername)) {
            wrapper.eq("vx_username", vxUsername);
        }
        return bizUserMapper.selectList(wrapper);
    }

    @Override
    public BizUser getBizUser(String vxId) {
        QueryWrapper<BizUser> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(vxId)) {
            wrapper.eq("vx_id", vxId);
        }
        List<BizUser> bizUsers = bizUserMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(bizUsers)) {
            return bizUsers.get(0);
        }
        return null;
    }

    @Override
    public int addBizUser(BizUser bizUser) {
        return bizUserMapper.insert(bizUser);
    }
}
